﻿using Syspetro.Core.DbAccessor;
using Syspetro.Core.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Syspetro.Core.AppDeal
{
    [Serializable]
    public abstract class PageReq<T> : IPageReq<T> where T : IPrivateEntity, new()
    {
        public PageReq()
        {
            Current = 1;
            Size = 10;
            WhereOptions = new List<LinqOption>();
        }
        public int Current { get; set; } = 1;
        public int Size { get; set; } = 10;
        public string OrderBy { get; set; }
        public string And_Or { get; set; } = "And";
        public List<LinqOption> WhereOptions { get; set; }
        public string Sorting { get; set; } = "normal";
        /// <summary>
        /// 查找条件
        /// </summary>
        public virtual Expression<Func<T, bool>> GetWhereExpression()
        {
            if (WhereOptions?.Count > 0)
            {
                if (And_Or == "And" || And_Or == "and")
                    return LinqHelper.CreateExpression<T>(WhereOptions);

                else
                    return LinqHelper.CreateExpression<T>(WhereOptions, ExpressionType.OrElse);
            }
            else
                return t => 1 == 1;
        }
        /// <summary>
        /// 排序字段
        /// </summary>
        /// <returns></returns>
        public virtual Expression<Func<T, object>> GetOrderByExpression()
        {
            if (string.IsNullOrEmpty(OrderBy))
                return null;
            return LinqHelper.CreateExpression<T>(OrderBy);
        }
        /// <summary>
        /// 是否升序
        /// </summary>
        /// <returns></returns>
        public virtual bool IsAscending()
        {
            return string.Compare(Sorting, "asc", true) == 0;
        }
    }
}
